#if MSC_VER >=1000
    #pragma execution_character_set("UTF-8")
#endif

#include "songlist.h"
#include<QTextStream>
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonParseError>
#include<QDebug>
#include<QJsonArray>
SongList::SongList()
{

}

SongList SongList::parseFromJSON(QString jstr)
{
    SongList result;
    QJsonDocument doc = QJsonDocument::fromJson(jstr.toLocal8Bit().data());

    if(doc.isNull()){
        qDebug()<<"JSONString null";
        return result;
    }

    QJsonObject jsonObject = doc.object();
    qDebug()<<jsonObject;

    auto scode =     jsonObject .value ("code");
    result.setCode(scode.toInt ());

    if (result .getCode () != 200){
        return result;
    }


    // bian li resutl
    auto sresult = jsonObject.value("result");

    auto sObjSongs =  sresult.toObject ();

    auto objArrSongs = sObjSongs.value ("songs").toArray ();


    for (auto e :objArrSongs){
        TSNGSong sng ;
        QJsonObject ev = e.toObject ();

        Album album;
        auto jobalbum = ev.value("album").toObject();

        auto jobartist = jobalbum.value ("artist").toObject ();
        // set artist
        Artists artist ;
        artist.setId(jobartist.value ("id").toString ().toLong ());
        artist.setImg1v1 (jobartist.value ("img1v1").toInt());
        artist.setImg1v1Url (jobartist.value ("img1v1Url").toString ());
        artist.setName(jobartist.value("name").toString ());
        artist.setPicId(jobartist.value ("picId").toString ().toLong());
        artist.setPicUrl (jobartist.value ("picUrl").toString());
        artist.setTrans (jobartist.value ("trans").toString());

        album.setArtist (artist);

        // set album
        album.setCopyrightId (jobalbum.value ("copyrightId").toString ().toLong ());
        album.setId (jobalbum.value ("id").toString ().toLong());
        album.setName(jobalbum.value ("name").toString());
        album.setPicId (jobalbum.value ("picId").toString ().toLong ());
        album.setPublishTime (jobalbum.value ("publishTime").toString().toLong ());
        album.setSize (jobalbum.value ("size").toInt ());

        sng.setAlbum (album);

        auto arrArtists = ev.value ("artists").toArray ();

        std::vector<Artists>arists;
        for (auto ar: arrArtists){
            Artists arist;
           auto aObj =  ar.toObject ();

          arist.setId (aObj.value ("id").toInt());
          arist.setImg1v1 (aObj.value ("img1v1").toInt());
          arist.setImg1v1Url (aObj.value ("img1v1Url").toString ());
          arist.setName (aObj.value ("name").toString());
          arist.setPicId (aObj.value ("picId").toInt());
          arist.setPicUrl (aObj.value ("picUrl").toString ());
          arists.push_back (arist);
        }

        sng.setArtists (arists);
        //setting number prod
        sng.setCopyrightId (ev.value ("copyrightId").toInt());
        sng.setDuration (ev.value ("duration").toInt());
        sng.setFee (ev.value ("fee").toInt ());
        sng.setFtype (ev.value ("ftype").toInt ());
        sng.setId (ev.value ("id").toInt());
        sng.setMvid (ev.value ("mvid").toInt());
        sng.setName (ev.value ("name").toString ());
        sng.setRUrl(ev.value ("rUrl").toString ());
        sng.setRtype (ev.value ("rtype").toInt());
        sng.setStatus(ev.value ("status").toInt ());

        result.getResult ().push_back (sng);
    }
    qDebug()<<"get data = "<<result.getResult ().size ();
    return result;
}



std::vector<TSNGSong> &SongList::getResult()
{
    return results;

}

void SongList::setCode(int code)
{
    this -> code  = code ;
}

int SongList::getCode()
{
    return this->code;
}
